/*
 * SOLMIX PROJECT
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.gnu.org/licenses/ 
 * or see the FSF site: http://www.fsf.org. 
 */

package org.solmix.web.box.client.ui;

import org.solmix.web.box.client.dnd.AppMenuDragController;
import org.solmix.web.box.client.dnd.AppMenuDropController;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 
 * @author Administrator
 * @version $Id$ 2012-2-13
 */

public class AppMenuBar extends DockLayoutPanel implements HasRightClickHandler
{

    public static final int WIDTH = 72;

    public AppMenuBar()
    {
        this(Unit.PX);
    }

    /**
     * @param unit
     */
    public AppMenuBar(Unit unit)
    {
        super(unit);
        sinkEvents(Event.ONMOUSEUP | Event.ONDBLCLICK | Event.ONCONTEXTMENU);
        setSize("72px", "100%");
        setStyleName("main-menu-bar");
        final Image image_1 = new Image("images/box/big.png");
        image_1.addMouseOverHandler(new MouseOverHandler() {

            @Override
            public void onMouseOver(MouseOverEvent event) {
                image_1.addStyleName("main-menu-image-over");

            }

        });
        image_1.addMouseOutHandler(new MouseOutHandler() {

            @Override
            public void onMouseOut(MouseOutEvent event) {
                image_1.removeStyleName("main-menu-image-over");

            }

        });
        image_1.setTitle("sdfsdfsfsefsf");
        image_1.setStyleName("main-menu-image");
        addNorth(image_1, 67.0);
        Image image_2 = new Image("images/box/appmarket.png");
        addSouth(image_2, 67.0);
        image_2.setSize("67px", "67px");

        // VerticalPanel verticalPanel = new VerticalPanel();
        AppMenuDragController dragc = new AppMenuDragController(RootPanel.get());
        VerticalPanel menuSpacer = new VerticalPanel();
        menuSpacer.setWidth("67px");
        Image w1 = new Image("images/box/10008.png");
        // w1.getElement().getStyle().setBackgroundColor("red");
        w1.setSize("65px", "65px");
        menuSpacer.setStyleName("main-menu-image");
        menuSpacer.add(w1);
        dragc.makeDraggable(w1);
        Image w2 = new Image("images/box/10002.png");
        w2.setSize("58px", "58px");
        menuSpacer.add(w2);
        dragc.makeDraggable(w2);
        Image w3 = new Image("images/box/10009.png");
        w3.setSize("58px", "58px");
        menuSpacer.add(w3);
        dragc.makeDraggable(w3);

        AppMenuDropController dropc = new AppMenuDropController(menuSpacer);
        dragc.registerDropController(dropc);
        add(menuSpacer);
    }

    private RightClickHandler handler;

    // DockLayoutPanel menuBarLayout = new DockLayoutPanel(Unit.PX);

    // public MainMenuBar()
    // {
    // // initWidget(menuBarLayout);
    //

    @Override
    public void onBrowserEvent(Event event) {

        GWT.log("mouseEvent fired!");
        // This will stop the event from being propagated to parent elements.
        // event.preventDefault();
        event.stopPropagation();

        switch (DOM.eventGetType(event)) {
            case Event.ONMOUSEUP:
                /*
                 * if (DOM.eventGetButton(event) == Event.BUTTON_LEFT) { GWT.log("Event.BUTTON_LEFT", null);
                 * handler.onClick(this, event); }
                 */
                if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT) {
                    GWT.log("Event.BUTTON_RIGHT", null);
                    handler.onRightClick(this, event);
                }
                break;

            case Event.ONDBLCLICK:

                break;

            case Event.ONCONTEXTMENU:
                GWT.log("Event.ONCONTEXTMENU", null);

                break;

            default:
                // DomEvent.fireNativeEvent(event, this, this.getElement());
        }// end switch
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.solmix.web.box.client.ui.HasRightClickHandler#addRightClickHandler(org.solmix.web.box.client.ui.RightClickHandler)
     */
    @Override
    public void addRightClickHandler(RightClickHandler handler) {
        // TODO Auto-generated method stub

    }
}
